import {
  Controller, UseFilters,
  Get,
  HttpException,
  HttpStatus,
} from '@nestjs/common'

// 自定义异常过滤器
import { CustomExceptionFilter } from './custom-exception.filter'

@Controller()
// @UseFilters( CustomExceptionFilter) // 控制器上使用自定义异常过滤器 应用于该控制器中所有路由处理函数
export class AppController {
  constructor(
  ) { }

  @Get('exception')
  handle() {
    // return 'provider'
    const data = new HttpException('禁止访问', HttpStatus.UNAUTHORIZED)
    console.log('Exception ', data.getResponse(), data.getStatus())
    throw data
  }

  @Get('exception2')
  handle2() {
    const data = new HttpException({ message: '禁止访问2', code: 402 }, HttpStatus.UNAUTHORIZED)
    console.log('Exception2 ', data.getResponse(), data.getStatus())
    throw data
  }

  @Get('exception3')
  handle3() {
    const data = new Error('自定义的错误')
    console.log('Exception3 ', data.message)
    throw data
  }

  @Get('exception4')
  @UseFilters( CustomExceptionFilter) // 路由处理函数上应用自定义异常过滤器 只针对该路由处理函数生效
  handle4() {
    throw new HttpException('出错了', 500)
  }

  @Get('exception5')
  // @UseFilters( CustomExceptionFilter)
  handle5() {
    throw new Error('又是一个出错')
  }
}
